package com.xiaoma.tpo.webservice;

import com.xiaoma.tpo.dao.UserWinNumDao;
import com.xiaoma.tpo.domain.Activity;
import com.xiaoma.tpo.domain.User;
import com.xiaoma.tpo.domain.UserKey;
import com.xiaoma.tpo.domain.UserWinNum;
import com.xiaoma.tpo.dto.UserKeyDto;
import com.xiaoma.tpo.service.ActivityService;
import com.xiaoma.tpo.service.UserKeyService;
import com.xiaoma.tpo.service.UserNumService;
import com.xiaoma.tpo.service.UserService;
import com.xiaoma.tpo.utils.TPOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Owner on 2015/5/12.
 */
@Controller
@RequestMapping("/service/key")
public class UserKeyWs {
    @Autowired
    private UserKeyService userKeyService;


    @Autowired
    private UserService userService;
    
    @Autowired
    private UserNumService userNumService;
    
    @Autowired
    private ActivityService activityService;
    
    @Autowired
    private UserWinNumDao userWinNumDao;

    @RequestMapping(value = "/addNum",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> addWinNum(@RequestHeader String token,@RequestBody UserKeyDto dto){
        Map<String, Object> result = new HashMap<String,Object>();
        User user = userService.findByToken(token);
        if(null == user) {
            result.put("status", -2);
            result.put("message", "invalid token");
            return result;
        }
        if (TPOUtils.isAnonymous(user)) {
            result.put("status", 0);
            result.put("message", "anonymous can't access");
            return result;

        }
        if (TPOUtils.over7days(new Date(), user.getLastVisit())) {//大于7天token失效
            result.put("status", -1);
            result.put("message", "token disabled over 7 days, please login");
            return result;
        }

        int status = userKeyService.addWinNum(user,dto.getActivityId());
        if(status==11){
            result.put("message","达到最大获奖次数");
        }else{
            result.put("message","success");
        }
        result.put("status",status);
        return  result;
    }

    @RequestMapping(value = "/generate",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> createKey(@RequestHeader String token,
    		@RequestBody UserKeyDto dto){
        Map<String, Object> result = new HashMap<String,Object>();
        User user = userService.findByToken(token);
        if(null == user) {
            result.put("status", -2);
            result.put("message", "invalid token");
            return result;
        }
        if (TPOUtils.isAnonymous(user)) {
            result.put("status", 0);
            result.put("message", "anonymous can't access");
            return result;

        }
        if (TPOUtils.over7days(new Date(), user.getLastVisit())) {//大于7天token失效
            result.put("status", -1);
            result.put("message", "token disabled over 7 days, please login");
            return result;
        }
        
        
        Date currentDate = new Date();
        //添加判断 领奖次数 和活动时间
        int count = userNumService.getWinNumByUser(user.getId());
        List<Activity> list = activityService.findAll();
        Activity activity = null;
        if(list.size() > 0) {
        	activity = list.get(0);
        }
        if(activity != null) {
        	if(currentDate.after(activity.getEndDate()) || currentDate.before(activity.getStartDate())) {
            	result.put("status", 0);
            	result.put("message", "活动已过期，欢迎下次参与");
            	return result;
            }else if(count >= 3) {
            	result.put("status", 0);
            	result.put("message", "已达到领奖上限，感谢您的参与");
            	return result;
            }else {
            	//添加获奖次数
            	UserWinNum wun = new UserWinNum();
                wun.setUserId(user.getId());
                wun.setActivityId(dto.getActivityId());
                wun.setCreateDate(new Date());
                userWinNumDao.insert(wun);
                
                //生成兑奖码
            	UserKey key = userKeyService.createKey(user);
                result.put("status",1);
                result.put("userKey",key);
                return result;
            }
        }else {
        	result.put("status", 0);
        	result.put("message", "活动已过期，欢迎下次参与");
        	return result;
        }
    }

    @RequestMapping(value = "/getkeys",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> getMyKeys(@RequestHeader String token,int page,int pageSize){
        Map<String, Object> result = new HashMap<String,Object>();
        User user = userService.findByToken(token);
        if(null == user) {
            result.put("status", -2);
            result.put("message", "invalid token");
            return result;
        }
        if (TPOUtils.isAnonymous(user)) {
            result.put("status", 0);
            result.put("message", "anonymous can't access");
            return result;

        }
        if (TPOUtils.over7days(new Date(), user.getLastVisit())) {//大于7天token失效
            result.put("status", -1);
            result.put("message", "token disabled over 7 days, please login");
            return result;
        }
        List<UserKey> list = userKeyService.getMyKeys(user, page, pageSize);
        result.put("status",1);
        result.put("items",list);

        return  result;
    }

    @RequestMapping(value = "/use",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> useKey(@RequestHeader String token,@RequestBody UserKeyDto dto){
        Map<String, Object> result = new HashMap<String,Object>();
        User user = userService.findByToken(token);
        if(null == user) {
            result.put("status", -2);
            result.put("message", "invalid token");
            return result;
        }
        if (TPOUtils.isAnonymous(user)) {
            result.put("status", 0);
            result.put("message", "anonymous can't access");
            return result;

        }
        if (TPOUtils.over7days(new Date(), user.getLastVisit())) {//大于7天token失效
            result.put("status", -1);
            result.put("message", "token disabled over 7 days, please login");
            return result;
        }
        int status = userKeyService.useKey(user,dto.getKey());
        if(status==0){
            result.put("status",1);
        }else{
            result.put("status",1+""+status);
            if(status==1){
                result.put("message","This key have been used");
            }else if (status==2){
                result.put("message","This key does not exist");
            }
        }

        return result;
    }
    
    @RequestMapping(value = "/actIdAndKeyCode",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> actIdAndKeyCode(@RequestHeader String token){
        Map<String, Object> result = new HashMap<String,Object>();
        User user = userService.findByToken(token);
        if(null == user) {
            result.put("status", -2);
            result.put("message", "invalid token");
            return result;
        }
        if (TPOUtils.isAnonymous(user)) {
            result.put("status", 0);
            result.put("message", "anonymous can't access");
            return result;
            
        }
        if (TPOUtils.over7days(new Date(), user.getLastVisit())) {//大于7天token失效
            result.put("status", -1);
            result.put("message", "token disabled over 7 days, please login");
            return result;
        }
        List<UserKey> userKeys = userKeyService.getLasted(user.getId());
        if (userKeys.size() > 0) {
            result.put("keyCode", userKeys.get(0).getKeyCode());
            List<UserWinNum> userWinNums = userNumService.findByUserId(user.getId());
            if (userWinNums.size() > 0) {
                result.put("activityId", userWinNums.get(0).getActivityId());
            } else {
                List<Activity> activities = activityService.findAll();
                if (activities.size() > 0) {
                    result.put("activityId",activities.get(0).getId());
                } else {
                    result.put("activityId",null);
                }
            }
        } else {
            result.put("keyCode", null);
            List<Activity> activities = activityService.findAll();
            if (activities.size() > 0) {
                result.put("activityId",activities.get(0).getId());
            } else {
                result.put("activityId",null);
            }
        }
        
        return result;
    }
}
